package com.yxzr.youyou.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxzr.youyou.MoviePlayActivity;
import com.yxzr.youyou.R;
import com.yxzr.youyou.bean.MovieDetailBean;

import java.util.ArrayList;
import java.util.List;

public class VideoDetailAdapter extends RecyclerView.Adapter<VideoDetailAdapter.ViewHolder> {
    private Context mContext;
    private int selectIndex = -1;
    private String index = "0";
    private ComicTypeAdapter.OnClickListener onClickListener;
    private List<MovieDetailBean.DataBean.MovieUrlBean> list = new ArrayList<>();
    public VideoDetailAdapter(Context context){
        this.mContext = context;
    }
    public void addList(List<MovieDetailBean.DataBean.MovieUrlBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.comictype_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position == selectIndex){
            holder.type_text.setBackgroundResource(R.drawable.type_text_shape);
            holder.type_text.setTextColor(Color.parseColor("#ffffff"));
        }else {
            holder.type_text.setBackgroundResource(R.drawable.gray_btn_shape);
            holder.type_text.setTextColor(Color.parseColor("#333333"));
        }
        if ("1".equals(index)){
            holder.type_text.setText(list.get(position).getJishu());
        }else {
            holder.type_text.setText("第"+list.get(position).getJishu()+"集");
        }

        holder.type_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setSelectIndex(int pos){
        this.selectIndex = pos;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView type_text;
        public ViewHolder(View itemView) {
            super(itemView);
            type_text = itemView.findViewById(R.id.type_text);
        }
    }
    public void setIndex(String index){
        this.index = index;
        notifyDataSetChanged();
    }
    public interface OnClickListener{
        void onClick(int position);
    }

    public void setOnClickListener(ComicTypeAdapter.OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }
}
